package com.cmpan.networkdisk.controller;

import com.cmpan.networkdisk.model.Disk;
import com.cmpan.networkdisk.model.Page;
import com.cmpan.networkdisk.model.Type;
import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.DiskService;
import com.cmpan.networkdisk.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ClassName:IndexController
 * Package:com.cmpan.networkdisk.controller
 * Description:
 *
 * @Date:2020/7/4 21:22
 * @com.chuangmei
 */
@Controller
public class IndexController {

    @Autowired
    private DiskService diskService;

    @Autowired
    private TypeService typeService;

    /*欢迎页*/
    @RequestMapping("/")
    public ModelAndView index(HttpServletRequest request){
        //ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("say","hello world");

        //获取session
        User user = (User) request.getSession().getAttribute("user");
        if (user != null){
            modelAndView.addObject("user",user);
        }

        return modelAndView;
    }

    /*跳转至登录界面*/
    @GetMapping("/loginIndex")
    public String loginIndex(){
        //System.out.println("进入登录界面");
        return "login";
    }

    //这个请求是受拦截的
    @GetMapping("/goDisk")
    public ModelAndView goDisk(HttpSession session,@RequestParam(required = false) String pagestart){

        //每页展示的数据
        int showpage = 10;

        //ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        //默认跳转登录界面
        modelAndView.setViewName("login");

        if (pagestart == null){
            pagestart = "1";
        }

        //pagestart
        int startpage = Integer.valueOf(pagestart);
        System.out.println("godisk: " + pagestart);

        //获取user
        User user = (User) session.getAttribute("user");
        //如果user不为null
        if (user != null){
            //登录成功查询出所有的file
            int totalpage = diskService.selectPage();
            List<Disk> allFile  = diskService.selectAllPage(startpage,showpage,totalpage);
            List<Type> allType = typeService.getAllType();
            Page page = new Page();
            page.setStartpage(startpage);
            page.setTotalpage(totalpage);
            System.out.println("页数: " + totalpage/showpage);
            //页数
            //如果每页展示的条数==总条数那么返回1页
            if (totalpage == showpage)
                page.setPagequantity(1);
            else if(totalpage%showpage == 0)
                page.setPagequantity((totalpage/showpage));
            else
                page.setPagequantity((totalpage/showpage) + 1);
            modelAndView.addObject("disk",allFile);
            modelAndView.addObject("user",user);
            modelAndView.addObject("types",allType);
            modelAndView.addObject("page",page);
            modelAndView.setViewName("disk");
        }

        return modelAndView;
    }

    //返回关于我们页面
    @GetMapping("/aboutus")
    public String about(){
        return "about";
    }



}
